// pages/balance/balance.js
var app = getApp();
const skinBehavior = require('../../../../utils/themeSkin/skinBehavior');

Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    mailLanguage: {},
    language: '',
    balance: 0,
    //是否开启充值
    recharge: 0,
    transfer: 0,
    withdraw: 0,
    //充值是否开放微信充值
    recharge_wx: 0,
    //充值是否开放支付宝充值
    recharge_alipay: 0,
    //提现到微信1-开 0-关
    withdrawToWechat: 0,
    withdrawToAlipay: 0,
    //余额
    member_credit2: 0.00,
    balanceLang: '余额',
    ios_virtual_pay: false,
    dataInfo: {}, //保存信息

    name: false,
    // 生态积分
    Navlis: 'day',
    nameOther: [{
        img: 'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2FecologicalPoint%2Frangli%402x.png',
        title: '让利奖励',
        id: 1
      },
      {
        img: 'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2FecologicalPoint%2Fjingxiaoshang%402x.png',
        title: '经销商奖励',
        id: 2
      },
      {
        img: 'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2FecologicalPoint%2Fquyu%402x.png',
        title: '区域奖励',
        id: 3
      }
    ],
    point_statistics: '',
    ecologicalName: '生态积分'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.name == 'cat') {
      // 生态积分
      this.setData({
        name: options.name,
      })
      this.ecological();
      return
    } else {
      this.getBalance();
    }
    try {
      var mailLanguage = JSON.parse(wx.getStorageSync('mailLanguage'));
      if (mailLanguage) {
        this.setData({
          mailLanguage: mailLanguage
        });
      }
    } catch (e) {
      console.log(e);
    }
    try {
      let value = wx.getStorageSync('ios_virtual_pay');
      if (value) {
        this.setData({
          ios_virtual_pay: value
        });
      }
    } catch (e) {
      console.log(e);
    }
    try {
      let value = wx.getStorageSync('yz_basic_info');
      if (value) {
        value = value.balance || "余额";
        this.setData({
          balanceLang: value
        });
        wx.setNavigationBarTitle({
          title: value
        });
      }
    } catch (e) {
      // Do something when catch error
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  tapNav(e) {
    console.log(e)
    this.setData({
      Navlis: e.currentTarget.dataset.key,
      point_statistics:[]
    })
    this.ecological();
  },
  ecological() {
    let urlStr = app.getNetAddresss("plugin.ecological-point.frontend.controllers.page.index");
    app._getNetWork({
      url: urlStr,
      data: {
        slot_type: this.data.Navlis
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res.data)
          let basic_info = wx.getStorageSync('yz_basic_info');
          this.setData({
            ecologicalName: basic_info.ecological_point ? basic_info.ecological_point : '生态积分',
            balanceLang: basic_info.ecological_point ? basic_info.ecological_point : '生态积分'
          })
          wx.setNavigationBarTitle({
            title: this.data.ecologicalName,
          })
          wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: this.data.themeBaseColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          });
          this.setData({
            dataInfo: res.data,
            point_statistics: res.data.point_statistics
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getBalance() {
    let that = this;
    let urlStr = app.getNetAddresss("finance.balance.index");
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            dataInfo: res.data,
          });
          wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: that.data.themeBaseColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  tourl(e) {
    let key = e.currentTarget.dataset.item;
    if (key == "Balance_recharge") {
      this.chargeHandel();
    } else if (key == "balance_transfer") {
      this.transferHandel();
    } else if (key == "balance_withdrawals") {
      this.withdrawHandel();
    } else if (key == 'ecologicalPointOther') {
      wx.navigateTo({
        url: '/packageI/ecologicalPoint/ecologicalPointOther/ecologicalPointOther?status=' + e.currentTarget.dataset.status,
      })
    }
  },
  transformingTo(e) {
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/packageI/ecologicalPoint/ecologicalPointAward/ecologicalPointAward?status=' + item.id,
    })
  },
  godetailed() {
    wx.navigateTo({
      url: '/packageA/member/balance/detailed/detailed'
    });
  },
  godetailed_point() {
    wx.navigateTo({
      url: '/packageI/ecologicalPoint/ecologicalPointDetailed/ecologicalPointDetailed',
    })
  },
  //充值的响应方法
  chargeHandel() {
    if (this.data.ios_virtual_pay) {
      wx.showModal({
        title: '提示',
        content: '十分抱歉，由于相关规定，你暂时无法在这里充值！',
        confirmText: '知道了',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            console.log(res);
          }
        }
      });
      return false;
    }

    wx.navigateTo({
      url: '/packageA/member/balance/balance_recharge/balance_recharge'
    });
  },
  //转账的响应方法
  transferHandel() {
    wx.navigateTo({
      url: '/packageA/member/balance/balance_transfer/balance_transfer?balance=' + this.data.member_credit2
    });
  },
  //提现的响应方法
  withdrawHandel() {
    wx.navigateTo({
      url: '/packageA/member/balance/balance_withdrawals/balance_withdrawals'
    });
  },
  //转化爱心值
  transformingLoveV() {
    wx.navigateTo({
      url: '/packageA/member/balance/transformLoveV/transformLoveV'
    });
  },
});